﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data;
using BusinessObjects.BO_GetObjects;
using System.IO;

/// <summary>
/// Summary description for Vote
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
[System.Web.Script.Services.ScriptService]
public class Vote : System.Web.Services.WebService
{

    public Vote()
    {
        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }


    [WebMethod]
    public void sendVote(int index)
    {
        try
        {
            string path = Server.MapPath("~/upload/xml/vote.xml");
            if (!File.Exists(path))
                return;
            DataSet objDS = new DataSet();
            objDS.ReadXml(path);
            DataTable objResult = objDS.Tables[1];
            DataTable objQuestion = objDS.Tables[0];
            if (objQuestion.Rows.Count > 0)
            {
                objQuestion.Rows[0]["TOTAL"] = Convert.ToInt32(objQuestion.Rows[0]["TOTAL"]) + 1;
            }
            string question = objQuestion.Rows[0]["QUESTION"].ToString();
            string val = string.Empty;
            for (int i = 0; i < objResult.Rows.Count; i++)
            {
                if (i == index)
                {
                    objResult.Rows[index]["NUMBER"] = Convert.ToInt32(objResult.Rows[index]["NUMBER"]) + 1;
                    objResult.Rows[index]["RATE"] = (Convert.ToInt64(objResult.Rows[index]["NUMBER"]) / Convert.ToDecimal(objQuestion.Rows[0]["TOTAL"])) * 100;
                }
                else
                {
                    objResult.Rows[i]["RATE"] = (Convert.ToInt64(objResult.Rows[i]["NUMBER"]) / Convert.ToDecimal(objQuestion.Rows[0]["TOTAL"])) * 100;
                }
            }
            objDS.WriteXml(path);
        }
        catch { }
    }
}
